# ============================================================================
# 人脸质量检测API服务 Docker镜像 - 优化版
# ============================================================================

# 阶段1: 构建阶段 - 包含编译工具
FROM python:3.11-slim AS builder

# 设置工作目录
WORKDIR /app

# 配置pip使用阿里云镜像源（加速Python包下载）
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 安装编译工具和系统依赖（仅安装必需的包）
RUN set -eux; \
    rm -f /etc/apt/sources.list.d/*; \
    rm -f /etc/apt/sources.list; \
    echo "deb http://mirrors.aliyun.com/debian/ trixie main contrib non-free non-free-firmware" > /etc/apt/sources.list; \
    echo "deb http://mirrors.aliyun.com/debian/ trixie-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list; \
    echo "deb http://mirrors.aliyun.com/debian-security trixie-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list; \
    apt-get clean; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        libgl1 \
        libglib2.0-0 \
        libgomp1; \
    rm -rf /var/lib/apt/lists/*





# 复制依赖文件
COPY requirements.txt .

# 先安装基础依赖（指定numpy版本避免冲突）
RUN pip install --no-cache-dir \
    "numpy<2.0,>=1.24.0" \
    opencv-python \
    onnxruntime

# 安装其他Python依赖
RUN pip install --no-cache-dir \
    fastapi==0.104.1 \
    "uvicorn[standard]==0.24.0" \
    python-multipart==0.0.6 \
    jinja2==3.1.2 \
    "pydantic>=2.5.0" \
    pydantic-settings \
    python-dotenv \
    loguru \
    httpx \
    aiofiles

# 安装insightface（最后安装，因为可能需要编译）
RUN pip install --no-cache-dir insightface || \
    (echo "⚠️  insightface安装失败，尝试使用--no-build-isolation" && \
     pip install --no-cache-dir --no-build-isolation insightface)

# ============================================================================
# 阶段2: 运行阶段 - 精简镜像
# ============================================================================
FROM python:3.11-slim

# 设置工作目录
WORKDIR /app

# 只安装运行时必需的系统依赖（精简版）
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgl1 \
    libglib2.0-0 \
    libgomp1 \
    && rm -rf /var/lib/apt/lists/*

# 从构建阶段复制Python包
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# 复制应用代码
COPY api.py .
COPY config.py .
COPY model_manager.py .
COPY face/ ./face/
COPY static/ ./static/

# 复制模型文件（如果存在）
COPY models/ ./models/

# 创建必要的目录
RUN mkdir -p ./uploads ./logs

# 设置环境变量
ENV PYTHONPATH=/app
ENV PYTHONUNBUFFERED=1
ENV INSIGHTFACE_HOME=/app/models
ENV LOG_LEVEL=INFO

# 创建非root用户运行应用（安全最佳实践）
RUN useradd -m -u 1000 appuser && \
    chown -R appuser:appuser /app

USER appuser

# 暴露端口
EXPOSE 8000

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health').read()" || exit 1

# 启动命令
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
